package com.example.online.servlet;

import com.example.online.entity.User;
import com.example.online.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    private final UserService SERVICE = new UserService();
    private final int LOCKTIME = 300;//单位 秒
    private final int ERRORNUMBER = 3;//单位 次

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        * 仍存在问题
        * 数据正确且可正常使用
        * 但是控制台输出中文会乱码  只有服务器运行时才会出现
        * idea java均无问题
        *
        * 原文  请输入账户
        * 不处理   猫炉路猫戮聯氓聟楼猫麓娄氓聫路(String)
        * req.setCharacterEncoding("utf-8");璇疯緭鍏ヨ处鍙?(String)
        * req.setCharacterEncoding("GBK");//鐠囩柉绶崗銉ㄥ閸欙拷(String)
        * */
        req.setCharacterEncoding("UTF-8");



        //获取前端表单提交的数据
        String name = req.getParameter("name");
        String pwd = req.getParameter("pwd");

        /*
        * 账户状态代码，待优化
        *   0表示账户正常（默认）
        *   1表示验证码错误
        *   2表示账户不存在
        *   3表示账户存在但已被锁定
        * */
        int number = 0;

        if(!CaptchaServlet.check(req, resp)){
            number = 1;
            req.setAttribute("Login",4);
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }

        //查看是否有该用户存在
        if(SERVICE.isExistByName(name) == 0){
            //没找到则刷新界面幷提示用户名不存在
            req.setAttribute("Login",1);
            req.getRequestDispatcher("/login.jsp").forward(req,resp);

            number = 2;
        }

        //查看该用户是否被锁定
        if(SERVICE.isLock(name) < LOCKTIME && number == 0 && CaptchaServlet.check(req, resp)){
            //若锁定则刷新界面幷提示该用户已被锁定
            req.setAttribute("Login",2);
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
            number = 3;
        }

        //查看密码是否正确
        if(SERVICE.login(name,pwd)  == 0 && number == 0){
            //若密码错误则将账户错误次数加1
            req.setAttribute("Login",3);
            SERVICE.updateLoginErrorAdd1(name);
            //查看错误次数是否到达上限
            if(SERVICE.findLoginError(name) >= ERRORNUMBER){
                //若到达上限则锁定账户
                SERVICE.lockOfName(name);
                //设置账户锁定提示
                req.setAttribute("lock",4);
                //清零错误次数
                SERVICE.updateLoginError0(name);
            }
            //设置密码错误提示
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }else if(SERVICE.login(name,pwd) == 1 && number == 0 && CaptchaServlet.check(req, resp)){
            //若登陆成功则获取账户对象交给前端jsp渲染
            SERVICE.updateLoginNumberAdd1(name);
            SERVICE.updateLoginError0(name);
            User user = SERVICE.getUser(name,pwd);
            req.setAttribute("User",user);
            //已成功转发至主页
            req.getRequestDispatcher("/home.jsp").forward(req,resp);
        }


    }
}
